<!--
 * @Descripttion: 文件说明
 * @version: V1.0
 * @Author: GW
 * @Date: 2022-05-16 17:36:40
 * @LastEditors: GW
 * @LastEditTime: 2022-05-31 17:04:43
-->
<template>
    <div style="height:100%">
        <a-input-search placeholder="科目编码" @change="onLeftTreeChange" />
        <div style="height:calc(100% - 40px);overflow:auto">
            <a-tree :tree-data="treeData" @select="onSelect" />
        </div>
    </div>
</template>
<script>
import { pageList } from "@/api/erp/accounting";
export default {
    props: {
        //是否需要 辅助科目分类
        isAuxacc: {
            type: Boolean,
            default: false
        }
    },
    data () {
        return {
            treeData: [],
            setTime: 1,
        }
    },
    created () {
        this.pageList();
    },
    methods: {
        //左树搜索
        onLeftTreeChange (v, e) {
            //延迟搜索
            if (this.setTime) {
                clearTimeout(this.setTime)
            }
            this.setTime = setTimeout(() => {
                this.pageList('treesearch', v.target.value);
            }, 500)
        },
        // 获取列表
        pageList (type, value) {
            let postData = {
                current: 1,
                size: -1
            }
            pageList(postData).then(response => {
                if (response.code === 200) {

                    let dataJson = response.data.records.map(v => {
                        return { ...v, key: v.id, title: v.code + ' ' + v.name }
                    });

                    console.log("this.$route.path:", this.$route.path);
                    if (this.$route.path == '/finance/accounting/clientMain') {
                        //辅助余额表 刷选末级
                        dataJson = dataJson.filter(item => item.isEndNode);
                    }

                    let filterData = [];
                    if (type && value && value != null && value != '') {
                        filterData = dataJson.filter(item => item.name.includes(value) || item.code.includes(value));
                    } else {
                        filterData = dataJson;
                    }


                    if (this.isAuxacc) {
                        //按辅助分类

                        // this.isAuxaccCustomer //客户往来
                        // this.isAuxaccDepartment //部门
                        // this.isAuxaccPerson //个人往来
                        // this.isAuxaccProject //项目
                        // this.isAuxaccSupplier //供应商往来

                        let array1 = _.cloneDeep(filterData).filter(item => item.isAuxaccCustomer);
                        array1.forEach(item => {
                            item.type = 1;
                        })

                        let array2 = _.cloneDeep(filterData).filter(item => item.isAuxaccSupplier);
                        array2.forEach(item => {
                            item.type = 2;
                        })

                        let array3 = _.cloneDeep(filterData).filter(item => item.isAuxaccPerson);
                        array3.forEach(item => {
                            item.type = 3;
                        })

                        let array4 = _.cloneDeep(filterData).filter(item => item.isAuxaccDepartment);
                        array4.forEach(item => {
                            item.type = 4;
                        })

                        let array5 = _.cloneDeep(filterData).filter(item => item.isAuxaccProject);
                        array5.forEach(item => {
                            item.type = 5;
                        })

                        this.treeData = [
                            {
                                title: '客户往来',
                                key: '0-0',
                                children: array1
                            },
                            {
                                title: '供应商往来',
                                key: '0-1',
                                children: array2,
                            },
                            {
                                title: '个人往来',
                                key: '0-2',
                                children: array3,
                            },
                            {
                                title: '部门往来',
                                key: '0-3',
                                children: array4,
                            },
                            {
                                title: '项目核算',
                                key: '0-4',
                                children: array5,
                            },
                        ]
                    } else {
                        this.treeData = this.treeClass(filterData);
                    }

                    console.log(this.treeData)
                }
            }).catch(error => { });
        },
        onSelect (e, v) {
            console.log(e, v.node.dataRef);
            if (e) {
                this.$emit('backData', v.node.dataRef)
            }
        },
        treeClass (data) {

            console.log(typeof data)
            if (typeof data != 'object') {
                console.error('树形data集合错误');
                return
            }
            let map = new Map();
            data.forEach(item => {
                map.set(item.id, item);
            });
            data.forEach((item, index) => {
                let parentItem = map.get(item.parentId);
                if (parentItem) {
                    if (!parentItem.children) {
                        parentItem.children = [item];
                    } else {
                        parentItem.children.push(item);
                    }
                }
            });

            return data.filter(res => res.parentId == 0);
        },
    }
}
</script>
